package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureCommentDAO;
import org.jetnuke.entities.PictureComment;

public class GenericPictureCommentDAO extends AutoKeys implements
		PictureCommentDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericPictureCommentDAO.class);

	public PictureComment retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureCommentModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		PictureComment s = new PictureComment();

		rs.next();
		fillPictureCommentFromResultSet(s, rs);

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillPictureCommentFromResultSet(PictureComment s,
			ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureCommentFromResultSet(PictureComment, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setMSG_AUTHOR(rs.getInt("MSG_AUTHOR"));
		s.setMSG_BODY(rs.getString("MSG_BODY"));
		s.setMSG_DATE(rs.getTimestamp("MSG_DATE"));
		s.setMSGID(rs.getInt("MSG_ID"));
		s.setPID(rs.getInt("PID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillPictureCommentFromResultSet(PictureComment, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			PictureComment u = new PictureComment();
			fillPictureCommentFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public int addNew(PictureComment pa) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureComment) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("PictureCommentModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("PictureCommentModel.lastGeneratedPictureCommentID"));
		initPictureComment(pa, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(PictureComment) - end"); //$NON-NLS-1$
		}
		return id;

	}

	protected void initPictureComment(PictureComment pa, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initPictureComment(PictureComment, PreparedStatement) - start"); //$NON-NLS-1$
		}

		GregorianCalendar gc = new GregorianCalendar();
		long initTime = gc.getTimeInMillis();
		Timestamp stamp = new Timestamp(initTime);

		p.setInt(1, pa.getMSG_AUTHOR());
		p.setString(2, pa.getMSG_BODY());
		p.setTimestamp(3, stamp);
		p.setInt(4, pa.getPID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initPictureComment(PictureComment, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public List selectByPicture(int pid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPicture(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PictureCommentModel.selectByPicture"));
		p.setInt(1, pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPicture(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectLastComments(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectLastComments(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("PictureCommentModel.selectLastComments"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectLastComments(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public void deleteByPID(int pid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("PictureCommentModel.deleteByPID"));
		p.setInt(1, pid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPID(int) - end"); //$NON-NLS-1$
		}
	}

}
